package org.ishequ.weibo.adapater;

import java.util.List;

import org.ishequ.miduo.R;
import org.ishequ.weibo.domain.UserInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class UserAdapater extends BaseAdapter {

	private List<UserInfo> ldata;
	private int resource;
	private LayoutInflater inflater;

	public UserAdapater(Context context, int resource,
			List<UserInfo> userInfoList) {
		this.ldata = userInfoList;
		this.resource = resource;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return ldata.size();
	}

	public Object getItem(int position) {
		return ldata.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = inflater.inflate(resource, null);
		bindView(position, convertView);
		return convertView;
	}

	private void bindView(int position, View view) {
		ImageView iv = (ImageView) view.findViewById(R.id.iconImg);
		TextView tv = (TextView) view.findViewById(R.id.showName);
		UserInfo user = ldata.get(position);
		try {
			// 设置图片显示
			iv.setImageDrawable(user.getUserIcon());
			// 设置信息
			tv.setText(user.getUserName());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}